clear all
cd "C:\Users\ot3\Documents\CRFertility\BirthData"

***birth data, from NBER***

*start with 1950-1967 from printed vitals books (1950 is first year with racial disaggregations)
use clean_natality1950, clear
keep if county=="total"
drop if race=="total"
keep year state race births 
save births_docs, replace


foreach y of numlist 1951/1967 {
use clean_natality`y', clear
keep if county=="total"
drop if race=="total"
keep year state race births 
append using births_docs
save births_docs, replace
}

merge m:1 state  using state_name_nchs_fips_xwalk 
keep if _merge==3
g black=race=="nonwhite"
keep state fipsst year black births
destring fipsst, replace
save births_docs, replace

*1968-1980 from microdata 
use natl1968 , clear
recode mrace(1=0) (nonmiss=1), gen(black) //black is actually non-white
collapse (count) births=crace2, by(stateres black) //just counts birth for obs with valid race variable 
g year=1968
rename stateres nchs_state
replace birth=birth*2 //all 50% samples through 1971
save births_docs_fmicrodata, replace


foreach y of numlist 1969/1971 {
use natl`y' , clear
recode mrace(1=0) (nonmiss=1), gen(black) //black is actually non-white
collapse (count) births=crace3, by(stateres black)
g year=`y'
rename stateres nchs_state
replace birth=birth*2 //all 50% samples through 1971 
append using births_docs_fmicrodata
save births_docs_fmicrodata, replace
}

foreach y of numlist 1972/1980 {
use natl`y' , clear
recode mrace(1=0) (nonmiss=1), gen(black) 
collapse (sum) births=recwt, by(stateres black) //summing recwt accounts for some 50% samples, some full counts
g year=`y'
rename stateres nchs_state
append using births_docs_fmicrodata
save births_docs_fmicrodata, replace
}


destring nchs_state, replace 
merge m:1 nchs_state using state_name_nchs_fips_xwalk 
keep if _merge==3
drop _merge nchs_state
save births_docs_fmicrodata, replace

append using births_docs

save births, replace
rm births_docs.dta
rm births_docs_fmicrodata.dta



***populations of fertile age females by race***

*1949
use 02896-0036-Data, clear
keep if level==2
tostring fips, format(%05.0f) replace
g fipsst=substr(fips,1,2)
destring fipsst, replace
egen fpop0=rowtotal(wf1519 wf2024 wf2529 wf3034 wf3539 wf4044)
egen fpop1=rowtotal(nwf1519 nwf2024 nwf2529 nwf3034 nwf3539 nwf4044)
collapse (sum) fpop0 fpop1, by(fipsst)
reshape long fpop, i(fipsst) j(black)
g year=1949
save c1949, replace

*1959
use 02896-0039-Data, clear
keep if level==2
tostring fips, format(%05.0f) replace
g fipsst=substr(fips,1,2)
destring fipsst, replace
egen fpop0=rowtotal(wf1519 wf2024 wf2529 wf3034 wf3539 wf4044)
egen fpop1=rowtotal(nwf1519 nwf2024 nwf2529 nwf3034 nwf3539 nwf4044)
collapse (sum) fpop0 fpop1, by(fipsst)
reshape long fpop, i(fipsst) j(black)
g year=1959
save c1959, replace


*1969-1980
use uswbo19agesadj, clear
keep if year<=1980 & sex==2 
keep if age>=4 & age<=9
destring stfips, g(fipsst)
recode race (1=0) (nonmiss=1), gen(black)
collapse (sum) fpop=pop, by(fipsst year black)

*append all together
append using c1949 
append using c1959

*impute non-census years pre 1969
reshape wide fpop, i(fips black) j(year)

g annualdiff=(fpop1959-fpop1949)/10
g fpop1950=fpop1949+annualdiff
g fpop1951=fpop1949+annualdiff*2
g fpop1952=fpop1949+annualdiff*3
g fpop1953=fpop1949+annualdiff*4
g fpop1954=fpop1949+annualdiff*5
g fpop1955=fpop1949+annualdiff*6
g fpop1956=fpop1949+annualdiff*7
g fpop1957=fpop1949+annualdiff*8
g fpop1958=fpop1949+annualdiff*9
drop annualdiff


g annualdiff=(fpop1969-fpop1959)/10
g fpop1960=fpop1959+annualdiff
g fpop1961=fpop1959+annualdiff*2
g fpop1962=fpop1959+annualdiff*3
g fpop1963=fpop1959+annualdiff*4
g fpop1964=fpop1959+annualdiff*5
g fpop1965=fpop1959+annualdiff*6
g fpop1966=fpop1959+annualdiff*7
g fpop1967=fpop1959+annualdiff*8
g fpop1968=fpop1959+annualdiff*9
drop annualdiff

reshape long fpop, i(fipsst black) j(year)


save pops, replace
rm c1949.dta
rm c1959.dta




*merge birth and pop data
use pops, clear
merge 1:1 fipsst year black using births
keep if _merge==3
drop _merge
drop if fpop<100
g brate=births/(fpop/1000)

drop if inlist(fipsst, 2,15) //AK & HI
g south=inlist(fipsst, 1, 5, 12, 13,  22, 28, 37, 45, 47, 48, 51)
g border= inlist(fipsst, 10, 21, 24, 40, 54) 

keep if  year>=1955 & year<=1975

cd "C:\Users\ot3\Documents\CRFertility"
save frate_working_state, replace




